public class Selection {
    public static void sort(Comparable[] a){
        // 选择排序
        int N = a.length;
        for (int i = 0; i < N; i++){
            // 将a[i]与之后元素中最小元素交换位置
            int min = i;  // 记录最小元素的位置
            for (int j = i+1; j < N; j++){
                if (less(a[j], a[min])) min = j;
            }
            exch(a, i ,min);  //交换a[i]与a[min]位置
        }
    }

    private static boolean less(Comparable v, Comparable w){
        return v.compareTo(w) < 0;
    }

    private static void exch(Comparable[] a, int i, int j){
        Comparable t = a[i];
        a[i] = a[j];
        a[j] = t;
    }
}
